package com.yh.correct.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.yh.correct.model.Condition;
import com.yh.correct.model.CorrectedPerson;
import com.yh.correct.model.LeavePersonnel;
import com.yh.correct.model.Page;
import com.yh.correct.model.ResultData;
import com.yh.correct.model.RuleBreak;
import com.yh.correct.service.ICorrectedPersonService;
import com.yh.correct.service.IJudicialDeptmentService;
import com.yh.correct.service.ILeavePersonnelService;

/**
 * 
 * 外出请假条控制层
 * @author swj
 *
 */

@RestController
@RequestMapping("/leave")
public class LeavePersonnelController {

	/**
	 * 日志记录
	 */
	private static Logger logger=LogManager.getLogger(LeavePersonnelController.class);
	/**
	 * 注入外出请假登记服务
	 */
	@Autowired
	private ILeavePersonnelService leavepersonnelservice;
	
	/**
	 * 注入司法所服务层接口
	 */
	@Autowired
	public IJudicialDeptmentService jud;
	
	 /**
	 * 注入矫正人员服务层接口
	 */
	@Autowired
	public ICorrectedPersonService cor;
	
	/**
	 * 显示请假登记页面
	 * @return
	 */
	@RequestMapping(value="/show")
	public ModelAndView toShow(){
		ModelAndView mv=new ModelAndView("/supervision/leave");
		
		return mv;
	}
	
	/**
	 * 增加外出请假登记信息
	 * @param leavepersonnel
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping(value="/add")
	public ResultData addLeavePersonnel(LeavePersonnel leavepersonnel,String jids,String pids,String dates,String startdates,String enddates) throws ParseException{
		int i=0;
		leavepersonnel.setJid(Integer.parseInt(jids));
		leavepersonnel.setPid(Integer.parseInt(pids));
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		leavepersonnel.setDate(sdf.parse(dates));
		leavepersonnel.setEnddate(sdf.parse(enddates));
		leavepersonnel.setStartdate(sdf.parse(startdates));
		try {
			i=leavepersonnelservice.addLeavePersonnel(leavepersonnel);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("增加成功");
		}else{
			rd.setCode(-1);
			rd.setMessage("增加失败");
		}
		logger.debug("增加外出请假登记+LeavePersonnelController");
		return rd;
		
	}
	
	
	/**
	 * 删除外出请假登记信息
	 * @param leavepersonnel
	 * @return
	 */
	@RequestMapping(value="/del")
	public ResultData delLeavePersonnel(LeavePersonnel leavepersonnel){
	    int i=0;
		try {
			 i=leavepersonnelservice.delLeavePersonnel(leavepersonnel);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    ResultData rd=new ResultData();
	    if(i>0){
	    	rd.setCode(0);
	    	rd.setMessage("删除成功");
	    }else{
	    	rd.setCode(-1);
	    	rd.setMessage("删除失败");
	    }
		return rd;	
	}
	
	
	/**
	 * 修改外出请假登记信息
	 * @param leavepersonnel
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/edit")
	public ResultData editLeavePersonnel(LeavePersonnel leavepersonnel,String dates,String startdates,String enddates) throws Exception{
		int i=0;
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		leavepersonnel.setDate(sdf.parse(dates));
		leavepersonnel.setEnddate(sdf.parse(enddates));
		leavepersonnel.setStartdate(sdf.parse(startdates));
		logger.info("修改外出请假登记记录id="+leavepersonnel.toString());
		i=leavepersonnelservice.editLeavePersonnel(leavepersonnel);
		 ResultData rd=new ResultData();
		    if(i>0){
		    	rd.setCode(0);
		    	rd.setMessage("修改成功");
		    }else{
		    	rd.setCode(-1);
		    	rd.setMessage("修改失败");
		    }
			return rd;	
	}
	
	
	
	/**
	 * 查询单条外出请假登记信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getidleave")
	public ResultData getIdLeavePersonnel(int id){
		logger.debug(id);
		LeavePersonnel lp=new LeavePersonnel();
		
		try {
			lp=leavepersonnelservice.getIdLeavePersonnel(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(lp!=null){
			rd.setCode(0);
			rd.setMessage("查询单个信息成功");
		}else{
			rd.setCode(-1);
			rd.setMessage("查询单个信息失败");
		}
		return null;
		
	}
	/**
	 * 模糊查询 查询条件（请假编号,矫正单位编号，服刑人员编号）
	 * @param pageSize
	 * @param pageNum
	 * @param condition
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/getlike")
	public ResultData getLikeLeavePersonnel(int pageSize,int pageNum, Condition condition) throws Exception{
		logger.info("查询外出请假信息条件"+condition);
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("pageSize",pageSize);
		m.put("pageNum",pageNum);
		m.put("cunit", condition.getCunit());
		m.put("cname", condition.getCname());
		m.put("cdate", condition.getCdate());
		Page page=leavepersonnelservice.getLikeLeavePersonnel(m);
		logger.info(page);
	    ResultData rd=new ResultData();
	    if(page!=null){
	    	List<Object> lp=new ArrayList();
	    	lp.add(page);
	    	rd.setCode(0);
	    	rd.setMessage("查询成功");
	    	rd.setData(lp);
	    }else{
	    	rd.setCode(-1);
	    	rd.setMessage("查询失败");
	    }
	    
		return rd;
	}

	/**
	 * 增加窗口跳转
	 * 
	 * @return
	 */
	@RequestMapping(value="/addskip")
	public ModelAndView addSkip(){
		return new ModelAndView("supervision/leave_add");
		
	}
	
	/**
	 * 修改页面跳转
	 * @return
	 */
	@RequestMapping(value="/editskip")
	public ModelAndView editSkip(){
		return new ModelAndView("supervision/leave_edit");
		
	}
	
	
	/**
	 * 矫正单位下拉框显示
	 * @return
	 */
	@RequestMapping(value="/jselect")
	public List<Object> jShowSelect(){
		List<Object> list=jud.listJudicialDeptments();
		for (Object object : list) {
			logger.info("ja列表"+object.toString());
		}
		return list;
		
	}
	
	/**
	 * 矫正人员下拉框显示
	 * @return
	 */
	@RequestMapping(value="/pselect")
	public List pShowSelect(){
		List<CorrectedPerson> list=null;
		try {
			list = cor.findCorrectedPerson();
			for (Object object : list) {
				logger.info("ps列表"+object.toString());
			}

		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return list;	
	}
	
	
	
	/**
	 * 获取外出请假登记详情
	 * 
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/detail")
	public ModelAndView getDetail(HttpServletRequest request, LeavePersonnel leavepersonnel, String sort) throws Exception {	
		Map<String, Object> m = leavepersonnelservice.getLeavePersonnelById(leavepersonnel);
		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("details", m);
		logger.info("查看详情" + m.toString());
		if ("look".equals(sort)) {
			return new ModelAndView("supervision/leave_look", rsMap);
		}
		return new ModelAndView("supervision/leave_edit", rsMap);
	}
	
	
	
	
	
}
